<?php
namespace WPSCS_COMPONENTS_PLUGIN;

/**
 * Scripts and Styles Class
 */
class Assets_wpscs_class {
    static $instance;
    function __construct() {

        if ( is_admin() ) {
            add_action( 'admin_enqueue_scripts', [ $this, 'register' ], 5 );
        } else {
            add_action( 'wp_enqueue_scripts', [ $this, 'register' ], 5 );
        }
    }

    /**
     * Register our app scripts and styles
     *
     * @return void
     */
    public function register() {
        $this->register_scripts( $this->get_scripts() );
        $this->register_styles( $this->get_styles() );
    }

    /**
     * Register scripts
     *
     * @param  array $scripts
     *
     * @return void
     */
    private function register_scripts( $scripts ) {
        foreach ( $scripts as $handle => $script ) {
            $deps      = isset( $script['deps'] ) ? $script['deps'] : false;
            $in_footer = isset( $script['in_footer'] ) ? $script['in_footer'] : false;
            $version   = isset( $script['version'] ) ? $script['version'] : WLPFUNMNGPLUGIN_VERSION;

            wp_register_script( $handle, $script['src'], $deps, $version, $in_footer );
        }
        
        // wp_register_script("global-vue-rename", '', [], '', true);
        //wp_add_inline_script();
        //wp_enqueue_script("wpcsc-component-app-hydrate");
    }

    /**
     * Register styles
     *
     * @param  array $styles
     *
     * @return void
     */
    public function register_styles( $styles ) {
        foreach ( $styles as $handle => $style ) {
            $deps = isset( $style['deps'] ) ? $style['deps'] : false;

            wp_register_style( $handle, $style['src'], $deps, WLPFUNMNGPLUGIN_VERSION );
        }
    }

    /**
     * Get all registered scripts
     *
     * @return array
     */
    public function get_scripts() {
        // $prefix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '.min' : '';

        $scripts = [
            'wpscs-baseplugin-tailwind' => [
                'src'       => "https://cdn.tailwindcss.com/3.4.16",
                'version'   => '3.4.16',
                'in_header' => true
            ],
            'wpscs-baseplugin-vendor' => [
                'src'       => WPCC_COMPONENT_PLUGIN_URL . 'assets/global.vue.prod.js',
                'version'   => filemtime( WPCC_COMPONENT_PLUGIN_PATH . '/assets/global.vue.prod.js' ),
                'in_header' => true
            ],
            'wpscs-baseplugin-frontend' => [
                'src'       => WPCC_COMPONENT_PLUGIN_URL . 'admin/admin/main.bundle.js',
                'version'   => filemtime( WPCC_COMPONENT_PLUGIN_PATH . '/admin/admin/main.bundle.js' ),
                'in_header' => true
            ]
        ];

        return $scripts;
    }

    /**
     * Get registered styles
     *
     * @return array
     */
    public function get_styles() {

        $styles = [
            'wpscs-baseplugin-style' => [
                'src' =>  WPCC_COMPONENT_PLUGIN_URL . 'admin/admin/main.css?ver=' ,
                'version'   => rand(1, 1000000)
            ]
        ];

        return $styles;
    }

    public static function init() {
        if ( ! Assets_wpscs_class::$instance ) {
            Assets_wpscs_class::$instance = new Assets_wpscs_class();
        }
        return Assets_wpscs_class::$instance;
    }

}
Assets_wpscs_class::init();